<?php
/**
 * Created by PhpStorm.
 * User: minsy
 * Date: 2016/12/21
 * Time: 09:38
 */

class RegularExpressionsTool{
    /**
     * 手机号码正则判断
     */
    public function regularPhoneNum($phone)
    {
        if (preg_match("/1[34578]{1}\d{9}$/", $phone)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 邮箱正则
     */
    public function regularEmail($email)
    {
        $pattern = "^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$";
        if (preg_match($pattern, $email)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 身份证正则判断
     */
    public function regularCardID($card_id)
    {
        if (strlen($card_id) != 18) {
            return false;
        }
        $set = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
        $ver = array('1', '0', 'x', '9', '8', '7', '6', '5', '4', '3', '2');
        $arr = str_split($card_id);
        $sum = 0;
        for ($i = 0; $i < 17; $i++) {
            if (!is_numeric($arr[$i])) {
                return false;
            }
            $sum += $arr[$i] * $set[$i];
        }
        $mod = $sum % 11;
        if (strcasecmp($ver[$mod], $arr[17]) != 0) {
            return false;
        }
        return true;
    }

    /**
     * 特殊字符正则
     * 前一个正则匹配 只含有汉字、数字、字母、下划线，下划线位置不限
     * 后一个正则匹配 emoji表情
     */
    public function regularSpecialCharacters($name){
        if (!preg_match("^[a-zA-Z0-9_\u4e00-\u9fa5]+$", $name) ||
            preg_match("/(\\\u[ed][0-9a-f]{3})/i", json_encode($name)) != 0
        ){
            return false;
        }else{
            return true;
        }
    }


}